<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
    <xsl:param name="DOTNET2"/>


	<xsl:template match="Class">
		<xsl:variable name="Name" select="Name"/>
        <xsl:variable name="HeaderInfo" select="HeaderInfo/ProjectName"/>
		<xsl:variable name="IsComposite" select="IsComposite"/>

/* 
 *
 * Active Record Style Code Generation
 *
 */
 

using System;
using Castle.ActiveRecord;
using Nullables;

using NHibernate.Expression;


namespace <xsl:value-of select="HeaderInfo/ProjectName"/>.ActiveRecord  
{

	[ Serializable ]
	<xsl:if test="IsNested='false'">
	<xsl:if test="IsComposite='false'">[ ActiveRecord ( "[<xsl:value-of select="DBName"/>]" ) ]</xsl:if>
	<xsl:if test="IsComposite='true'">[ CompositeKey ]</xsl:if>
	</xsl:if>
	public class <xsl:value-of select="Name"/><xsl:if test="IsNested='false'"> : <xsl:value-of select="HeaderInfo/ProjectName"/>.ActiveRecord.DataObject</xsl:if>
	{

		
	
		#region Fields
		
		// single fields
		<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single']">
		<xsl:value-of select="Type"/><xsl:text> </xsl:text>_<xsl:value-of select="Name"/><xsl:if test="Type='DateTime'"> = DateTime.Now</xsl:if>;
		</xsl:for-each>
		
		// array fields
		<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array']">
		[ System.Xml.Serialization.XmlArrayItem ( typeof ( <xsl:value-of select="Type"/> ) ) ]
		Iesi.Collections.ISet<xsl:text> </xsl:text>_<xsl:value-of select="Name"/> = new Iesi.Collections.ListSet ();
		</xsl:for-each>
		
		
		
		#endregion
		
		
		#region Properties
		
		<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single']">
		<xsl:if test="$IsComposite='false'">
		<xsl:if test="Key='true'">[ PrimaryKey ( PrimaryKeyType.Assigned , "<xsl:value-of select="DBName"/>" ) ]</xsl:if>
		<xsl:if test="Identity='true'">[ PrimaryKey ( PrimaryKeyType.Identity ) ]</xsl:if>
		<xsl:if test="Key='false' and Identity='false' and KnownClass='false' and IsNested='false'">[ Property ( "<xsl:value-of select="DBName"/>" ) ]</xsl:if>
		<xsl:if test="Key='false' and Identity='false' and KnownClass='true' and IsNested='false'">[ BelongsTo ( "<xsl:value-of select="DBName"/>" , Type=typeof ( <xsl:value-of select="Type"/> ) ) ]</xsl:if>
		</xsl:if>
		<xsl:if test="$IsComposite='true'">[ KeyProperty ( Column="<xsl:value-of select="DBName"/>" ) ]</xsl:if>
		<xsl:if test="IsNested='true'">[ Nested ]</xsl:if>
		public <xsl:value-of select="Type"/><xsl:text> </xsl:text><xsl:value-of select="PropertyName"/>
		{
			get
			{
				return this._<xsl:value-of select="Name"/>;
			}
			
			set
			{
				this._<xsl:value-of select="Name"/> = value;
			}
		}
		
		</xsl:for-each>
		
		
		<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array']">
		<xsl:if test="HasMany='false'">
		[ HasAndBelongsToMany ( typeof ( <xsl:value-of select="Type"/> ) ,
		   Table = "<xsl:if test="TableName=''"><xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/></xsl:if><xsl:if test="TableName!=''"><xsl:value-of select="TableName"/></xsl:if>",
		   ColumnRef = "<xsl:if test="ColumnRef=''"><xsl:value-of select="$Name"/>ID</xsl:if><xsl:if test="ColumnRef!=''"><xsl:value-of select="ColumnRef"/></xsl:if>", ColumnKey="<xsl:if test="ColumnKey=''"><xsl:value-of select="Type"/>bID</xsl:if><xsl:if test="ColumnKey!=''"><xsl:value-of select="ColumnKey"/></xsl:if>" 
		   <xsl:if test="Where!=''">, Where="<xsl:value-of select="Where"/>"</xsl:if> , Lazy=true ) ]
		</xsl:if>
		<xsl:if test="HasMany='true'">[ HasMany ( typeof ( <xsl:value-of select="Type"/> ) , Table="<xsl:value-of select="TableName"/>" , ColumnKey = "<xsl:value-of select="ColumnKey"/>" , Lazy=true ) ]</xsl:if>
		public Iesi.Collections.ISet <xsl:value-of select="PropertyName"/>
		{
			get
			{
				return this._<xsl:value-of select="Name"/>;
			}
			
			set
			{
				this._<xsl:value-of select="Name"/> = value;
			}
		}
		
		</xsl:for-each>
		
		#endregion
		
		
		
		
		<xsl:if test="IsComposite='true'">
		
		#region Composite Overrides
		
		public override bool Equals ( object obj )
		{
			if ( obj == this ) return true;
			if ( obj == null || obj.GetType() != this.GetType () ) return false;
			
			<xsl:value-of select="Name"/> objAsComp = ( <xsl:value-of select="Name"/> ) obj;
			
			return objAsComp.GetHashCode().Equals ( this.GetHashCode () );
		}
		
		
		public override int GetHashCode ()
		{
		<xsl:for-each select="ClassAttribs/ClassAttributes">
			<xsl:if test="position()=1">	return <xsl:value-of select="PropertyName"/>.GetHashCode() ^ </xsl:if>
			<xsl:if test="position()!=1 and position!=count(//ClassAttribs/ClassAttributes)"><xsl:value-of select="PropertyName"/>.GetHashCode() ^ </xsl:if>
			
			<xsl:if test="position()=count(//ClassAttribs/ClassAttributes)"><xsl:value-of select="PropertyName"/>.GetHashCode();</xsl:if>
		</xsl:for-each>
		}
		
		
		#endregion
		
		</xsl:if>
		
		
		
		
		<xsl:if test="IsComposite='false' and IsNested='false'">
		#region Active Record Accessors
		
		public static int CountAll ( )
		{
			return DataObject.ARCountAll ( typeof ( <xsl:value-of select="Name"/> ) );
		}
		
		public static int CountAll ( string filter, params object[] args )
		{
			return DataObject.ARCountAll ( typeof ( <xsl:value-of select="Name"/> ) , filter , args );
		}
		
		public static void DeleteAll ( )
		{
			DataObject.ARDeleteAll ( typeof ( <xsl:value-of select="Name"/> ) );
		}
		
		public static void DeleteAll ( string where )
		{
			DataObject.ARDeleteAll ( typeof ( <xsl:value-of select="Name"/> ) , where );
		}
		
		public static <xsl:value-of select="Name"/>[] FindAll ( )
		{
			return (<xsl:value-of select="Name"/>[])DataObject.ARFindAll ( typeof ( <xsl:value-of select="Name"/> ) );
		}
		
		public static <xsl:value-of select="Name"/>[] FindAll ( params ICriterion[] criterias )
		{
			return (<xsl:value-of select="Name"/>[])DataObject.ARFindAll ( typeof ( <xsl:value-of select="Name"/> ) , criterias );
		}
		
		public static <xsl:value-of select="Name"/> FindByPrimaryKey ( object id )
		{
		
			return DataObject.ARFindByPrimaryKey ( typeof ( <xsl:value-of select="Name"/> ) , id )
				as <xsl:value-of select="Name"/>;
		}
		
		
		public static <xsl:value-of select="Name"/> FindByPrimaryKey ( object id , bool throwOnNotFound )
		{
			return DataObject.ARFindByPrimaryKey ( typeof ( <xsl:value-of select="Name"/> ) , id , throwOnNotFound )
			 as <xsl:value-of select="Name"/>;
		}
		
		
		public static <xsl:value-of select="Name"/> FindFirst ( params ICriterion[] criterias )
		{
			return DataObject.ARFindFirst ( typeof ( <xsl:value-of select="Name"/> ) , criterias )
				as <xsl:value-of select="Name"/>;
		}
		
		public new object Execute ( NHibernateDelegate call )
		{
			return base.Execute ( call );
		}
		
		public static object ExecuteQuery ( IActiveRecordQuery query )
		{
			return DataObject.ARExecuteQuery ( query );
		}
		
		
		
		
		
		#endregion
		</xsl:if>
		
	
		
		
		
	}


}


	</xsl:template>

</xsl:stylesheet>



